package com.gxa.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Component
@Aspect
@EnableAspectJAutoProxy
public class FileUploadAOP {

    private static Logger logger = LoggerFactory.getLogger(FileUploadAOP.class);
    @Pointcut("@annotation(com.gxa.common.annotation.FileUploadCheck)")
    public void annotationPointCut() {
    }

    @Before("annotationPointCut()")
    public void beforeUpload(JoinPoint joinPoint) {
        System.out.println("===================================切面====================================");
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("request: "+request);
        System.out.println("request Params :"+request.getParameterNames());
        System.out.println("获取所有request参数：");
        //获取所有参数方法一：
        Enumeration<String> enu=request.getParameterNames();
        System.out.println("getParameterNames()：" + enu.hasMoreElements());
        System.out.println("joinPoint.getArgs():   " +joinPoint.getArgs());
        while(enu.hasMoreElements()){
            String paraName=enu.nextElement();
            System.out.println("paraName =》"+paraName+"  =》  "+request.getParameter(paraName));
        }
    }
}
